﻿using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Management;

namespace PackToKindle
{
    /// <summary>
    /// http://stackoverflow.com/questions/6003822/how-to-detect-a-usb-device-has-been-plugged-in
    /// http://stackoverflow.com/a/12932998/850334
    /// http://stackoverflow.com/questions/3331043/get-list-of-connected-usb-devices
    /// 
    /// http://www.icsharpcode.net/OpenSource/SharpUSBLib/default.aspx
    /// 
    /// http://www.codeproject.com/Tips/349002/Select-a-USB-Serial-Device-via-its-VID-PID
    /// http://code.google.com/p/usb-devices-list/
    /// http://code.google.com/p/usb-devices-list/
    /// http://www.gotdotnet.ru/forums/2/60240/
    /// </summary>
    internal class DeviceDetector
    {
        public static IKindle GetKindle()
        {
            var deviceList = _GetUSBDevices();

            var str = _GetUSBDevices222();

            var фывьь = _GetKindleD();

            // Select device from list.
            return null;
        }

        private static IKindle _GetKindleD()
        {
            var drives = DriveInfo.GetDrives()
                .Where(drive => drive.IsReady && drive.DriveType == DriveType.Removable);

            var first = drives.First();


            return null;
        }

        //http://blogs.technet.com/b/heyscriptingguy/archive/2005/03/15/how-can-i-determine-which-usb-devices-are-connected-to-a-computer.aspx
        //http://kodefumaster.wordpress.com/2009/05/09/getting-the-usb-devices-information-using-wmi/
        //http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/09c9814a-38fa-4b16-bc8f-01329882a791/
        // http://www.c-sharpcorner.com/uploadfile/puranindia/windows-management-instrumentation-in-C-Sharp/
        private static IEnumerable<USBDeviceInfo> _GetUSBDevices()
        {
            List<USBDeviceInfo> devices = new List<USBDeviceInfo>();

            ManagementObjectCollection collection;
            SelectQuery selectQuery = new
                SelectQuery("Win32_USBHub");
                //SelectQuery("Win32_USBControllerDevice");

            //using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"))
            using (var searcher = new ManagementObjectSearcher(selectQuery))
                collection = searcher.Get();

            foreach (var device in collection)
            {
                devices.Add(new USBDeviceInfo(
                (string)device.GetPropertyValue("DeviceID"),
                (string)device.GetPropertyValue("PNPDeviceID"),
                (string)device.GetPropertyValue("Description")
                ));
            }

            collection.Dispose();
            return devices;
        }
        private static IEnumerable<string> _GetUSBDevices222()
        {
            List<USBDeviceInfo> devices = new List<USBDeviceInfo>();

            ManagementObjectCollection collection;
            SelectQuery selectQuery = new
                //SelectQuery("Win32_USBHub");
                SelectQuery("Win32_USBControllerDevice");

            //using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"))
            using (var searcher = new ManagementObjectSearcher(selectQuery))
                collection = searcher.Get();

            var result = new List<string>();
            foreach (var device in collection)
            {
                result.Add(device.ToString());
            }

            collection.Dispose();
            return result;
        }
    }

    internal class USBDeviceInfo
    {
        public USBDeviceInfo(string deviceID, string pnpDeviceID, string description)
        {
            this.DeviceID = deviceID;
            this.PnpDeviceID = pnpDeviceID;
            this.Description = description;
        }

        public string DeviceID { get; private set; }
        public string PnpDeviceID { get; private set; }
        public string Description { get; private set; }
    }
}